<template>
  <div class="login-container">
    <div class="login-content">
      <el-card class="login-card">
        <div class="login-header">
          <div class="logo-wrapper">
            <div class="logo-circle">
              <el-icon size="32"><Lock /></el-icon>
            </div>
          </div>
          <h2 class="title">权限管理系统</h2>
          <p class="subtitle">企业级权限管理解决方案</p>
        </div>

        <el-form
          ref="formRef"
          :model="form"
          :rules="rules"
          class="login-form"
          @submit.prevent="handleLogin"
          autocomplete="off"
        >
          <el-form-item prop="username">
            <el-input
              v-model="form.username"
              :prefix-icon="User"
              placeholder="请输入用户名"
              size="large"
              clearable
              autocomplete="off"
              @keyup.enter="handleLogin"
            />
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input
              v-model="form.password"
              :prefix-icon="Lock"
              type="password"
              placeholder="请输入密码"
              size="large"
              show-password
              clearable
              autocomplete="new-password"
              @keyup.enter="handleLogin"
            />
          </el-form-item>

          <div class="remember-forgot">
            <el-checkbox v-model="rememberMe">记住我</el-checkbox>
            <el-link type="primary" :underline="false">忘记密码？</el-link>
          </div>

          <el-form-item>
            <el-button
              type="primary"
              :loading="loading"
              class="login-button"
              size="large"
              @click="handleLogin"
            >
              {{ loading ? '登录中...' : '登录系统' }}
            </el-button>
          </el-form-item>
        </el-form>
      </el-card>

      <div class="footer">
        <p>Copyright © 2024 权限管理系统 All Rights Reserved.</p>
      </div>
    </div>

    <div class="login-background">
      <div class="shape shape1"></div>
      <div class="shape shape2"></div>
      <div class="shape shape3"></div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../stores/user'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'

const router = useRouter()
const userStore = useUserStore()
const formRef = ref(null)
const loading = ref(false)
const rememberMe = ref(false)

const form = ref({
  username: '',
  password: ''
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!formRef.value) return

  try {
    await formRef.value.validate()
    loading.value = true
    const success = await userStore.login(form.value)
    
    if (success) {
      ElMessage.success('登录成功')
      router.push('/dashboard')
    } else {
      ElMessage.error('用户名或密码错误')
    }
  } catch (error) {
    console.error('Login error:', error)
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  position: relative;
  height: 100vh;
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  background-color: #f0f2f5;
}

.login-content {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 420px;
  padding: 0 20px;
}

.login-card {
  backdrop-filter: blur(10px);
  background: rgba(255, 255, 255, 0.95);
  border: none;
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease;
}

.login-card:hover {
  transform: translateY(-5px);
}

.login-header {
  text-align: center;
  padding: 20px 0;
}

.logo-wrapper {
  margin-bottom: 24px;
}

.logo-circle {
  width: 64px;
  height: 64px;
  margin: 0 auto;
  background: linear-gradient(135deg, #3494e6, #ec6ead);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  box-shadow: 0 4px 12px rgba(52, 148, 230, 0.3);
}

.title {
  margin: 0;
  font-size: 24px;
  font-weight: 600;
  color: #303133;
}

.subtitle {
  margin: 8px 0 0;
  font-size: 14px;
  color: #909399;
}

.login-form {
  padding: 20px 30px 30px;
}

.remember-forgot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.login-button {
  width: 100%;
  height: 44px;
  font-size: 16px;
  border-radius: 22px;
  background: linear-gradient(135deg, #3494e6, #ec6ead);
  border: none;
  font-weight: 500;
  letter-spacing: 1px;
}

.login-button:hover {
  background: linear-gradient(135deg, #2d7fc9, #e44f97);
  transform: translateY(-1px);
}

.footer {
  text-align: center;
  margin-top: 24px;
  color: #909399;
  font-size: 14px;
}

.login-background {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, #3494e6, #ec6ead);
  z-index: 0;
}

.shape {
  position: absolute;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 50%;
  animation: float 20s infinite;
}

.shape1 {
  width: 500px;
  height: 500px;
  top: -250px;
  right: -100px;
  animation-delay: 0s;
}

.shape2 {
  width: 400px;
  height: 400px;
  bottom: -150px;
  left: -100px;
  animation-delay: -5s;
}

.shape3 {
  width: 300px;
  height: 300px;
  bottom: 50%;
  right: 20%;
  animation-delay: -10s;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0) rotate(0deg);
  }
  25% {
    transform: translateY(-20px) rotate(5deg);
  }
  50% {
    transform: translateY(0) rotate(10deg);
  }
  75% {
    transform: translateY(20px) rotate(5deg);
  }
}

:deep(.el-input__wrapper) {
  background-color: #f5f7fa;
  border-radius: 22px;
  box-shadow: none;
  border: 1px solid transparent;
  transition: all 0.3s;
}

:deep(.el-input__wrapper:hover) {
  border-color: #3494e6;
}

:deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px #3494e6;
  border-color: #3494e6;
}

:deep(.el-form-item) {
  margin-bottom: 20px;
}

:deep(.el-form-item:last-child) {
  margin-bottom: 0;
}

:deep(.el-checkbox__label) {
  color: #606266;
}

:deep(.el-checkbox__input.is-checked .el-checkbox__inner) {
  background-color: #3494e6;
  border-color: #3494e6;
}

/* 添加下拉菜单样式 */
:deep(.el-dropdown-menu) {
  min-width: 100px !important;
}

:deep(.el-dropdown-menu__item) {
  padding: 8px 20px;
}
</style> 